/*
 * @Author: LiZn
 * @Date: 2021-08-21 07:14:22
 * @LastEditTime: 2021-08-22 23:28:02
 * @LastEditors: LiZn
 * @Description: 存储用户角色信息
 * @FilePath: \todaynews\src\store\user.js
 */
import axios from '../utils/request'
const state = {
  roles:'',
  userInfos:'',
  userToken:localStorage.getItem('userToken')
};
const mutations = {
  setRoles(state,roles){
    state.roles = roles
  },
  setUserToken(state,token){
    state.userToken = token
  },
  setUserInfos(state,userinfos){
    state.userInfos = userinfos
  }
}
const actions = {
  //登录获取用户信息
  login({commit},userinfos){
    return axios.post('/user/login',userinfos).then(res=>{
      console.log(res)
      //设置用户token
      if(res.code==2){
        commit('setUserToken',res.data.token);
        localStorage.setItem('userToken',res.data.token)
      }
      return res
    })
  },
  getUserInfo({commit},userId){
    return axios.post('/user/usersinfo',{token:userId}).then(res=>{
      commit('setRoles',res.data.roles);//将用户角色暂存在store中
      commit('setUserInfos',res.data.username);
      localStorage.setItem('username',res.data.username)
      return res.data.roles
    })
  }
}
export default {
  namespaced:true,
  state,
  mutations,
  actions
}
